<?php
/**
 * Created by PhpStorm
 * Author Hui
 * Date 2024/5/19   0:21
 */

namespace designpatterns;

use designpatterns\tool\Env;

class App extends Container
{
    //加載容器，加載配置文件。

    /**
     * 应用根目录
     * @var string
     */
    protected $rootPath = '';


    protected $envName = '';

    /**
     * 容器绑定标识
     * @var array
     */
    protected $bind = [
        'app' => App::class,
        'env' => Env::class,
    ];


    public function __construct(string $rootPath = '')
    {
        $this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath();
    }


    public function loadEnv(string $envName = '') : void
    {
        $file = $envName ? $this->rootPath . '.env.' . $envName : $this->rootPath . '.env';
        if(is_file($file)){
            (new Env())->load($file);
        }
    }

    /**
     * 初始化
     * @return void
     */
    public function initialize()
    {
        //加载配置
        $this->loadEnv($this->envName);
    }

    public function getDefaultRootPath()
    {
        return dirname(__DIR__).DIRECTORY_SEPARATOR;
    }


}